package model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import laWa.SusanCanvas;
import laWa.SusanOptions;

public class GameFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2601588613080786213L;
	
	private static final int DEFAULT_WIDTH = 1024;
	private static final int DEFAULT_HEIGHT = 768;
	
	SusanCanvas c;
	SusanOptions so;
	
	Board board = new Board();
	
	public Board getBoard() {
		return board;
	}

	public void init() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		so=new SusanOptions();
		so.showFortress = true;
		so.playmode = SusanOptions.playmode_pc;
		// SusanCanvas
	    c=new SusanCanvas(so);
	    
	    c.newGame(SusanOptions.begin_random);
	    //c.sgml.add(this);
		//add(board);
	    
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Susan - Board Game");
		
		setVisible(true);
		
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		//setExtendedState(JFrame.MAXIMIZED_BOTH);
		
		createMenu();
		add(c);
	}
	
	public void createMenu() {
		JMenuBar menu = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		
		JMenu newGameMenu = new JMenu("New Game");
		
		JMenuItem newGameComputer = new JMenuItem("Vs Computer");
		Actions newGameComputerAction = new Actions("newGameComputerAction");
		newGameComputer.addActionListener(newGameComputerAction);
		
		JMenuItem newGameHuman = new JMenuItem("Vs Human");
		Actions newGameHumanAction = new Actions("newGameHumanAction");
		newGameHuman.addActionListener(newGameHumanAction);
		
		newGameMenu.add(newGameComputer);
		newGameMenu.add(newGameHuman);
		
		JMenuItem quit = new JMenuItem("Quit Game");
		Actions quitGameAction = new Actions("quitGameAction");
		quit.addActionListener(quitGameAction);
		
		fileMenu.add(newGameMenu);
		fileMenu.add(quit);
		fileMenu.setMnemonic(KeyEvent.VK_F);
		menu.add(fileMenu);
		menu.add(Box.createHorizontalGlue());
		this.setJMenuBar(menu);
	}
	
	private class Actions implements ActionListener{
		private String s1;
		
		public Actions(String s) {
			s1 = s;
		}
		
		public void actionPerformed(ActionEvent event) {
			if (s1 == "quitGameAction") {
				System.exit(0);
			}
			else if (s1 == "newGameComputerAction") {
				so.playmode = SusanOptions.playmode_pc;
				c.newGame(SusanOptions.begin_random);
				c.repaint();
			}
			else if (s1 == "newGameHumanAction") {
				so.playmode = SusanOptions.playmode_pl;
				c.newGame(SusanOptions.begin_random);
				c.repaint();
			}
		}
	}
}
